//
//  SFCError.m
//  SVNForCocoa
//
//  Created by Jeremy Pereira on 06/11/2013.
//  Copyright (c) 2013 Jeremy Pereira. All rights reserved.
//

#import "SFCError.h"
#import <apr-1/apr_errno.h>
#import <svn_error.h>

NSString* const APR_ERROR_DOMAIN = @"net.jeremyp.apr";
NSString* const SFC_ERROR_DOMAIN = @"net.jeremyp.sfc";
NSString* const SVN_ERROR_DOMAIN = @"net.jeremyp.svn";


@implementation SFCError

+(NSError*) errorWithDomain: (NSString*) domain
                       code: (int) code
       localizedDescription: (NSString*) description
{
    return [[self class] errorWithDomain: domain
                                    code: code
                                userInfo: @{ NSLocalizedDescriptionKey : description } ];
}


+(NSError*) aprError: (int) aprStatus
{
    char stringBuffer[200];

	NSString* localisedDescription = [NSString stringWithUTF8String:
                                      apr_strerror(aprStatus,
                                                   stringBuffer,
                                                   sizeof stringBuffer)];
    NSDictionary* userInfo = @{ NSLocalizedDescriptionKey: localisedDescription};
    return [[self class] errorWithDomain: APR_ERROR_DOMAIN
                                    code: aprStatus
                                userInfo: userInfo];
}

+(NSError*) svnError: (svn_error_t*) svnError
{
    char stringBuffer[200];

	NSString* localisedDescription = [NSString stringWithUTF8String:
                                      svn_err_best_message(svnError,
                                                           stringBuffer,
                                                           sizeof stringBuffer)];
    NSDictionary* userInfo = @{ NSLocalizedDescriptionKey: localisedDescription};
    apr_status_t errorNumber = svnError->apr_err;
    NSString* domain = SVN_ERROR_DOMAIN;
    if (errorNumber > SVN_ERR_LAST)
    {
		domain = SFC_ERROR_DOMAIN;
        errorNumber -= SVN_ERR_LAST - 1;
    }
    return [[self class] errorWithDomain: domain
                                    code: errorNumber
                                userInfo: userInfo];

}

+(svn_error_t*) svnErrorFromError: (NSError*) anError
{
    apr_status_t svnErrorCode = (apr_status_t)[anError code];
    if ([NSPOSIXErrorDomain isEqualToString: [anError domain]])
    {
		svnErrorCode = APR_FROM_OS_ERROR(svnErrorCode);
    }
    else if (![SVN_ERROR_DOMAIN isEqualToString: [anError domain]])
    {
		svnErrorCode += SVN_ERR_LAST + 1;
    }
    svn_error_t* ret = svn_error_create(svnErrorCode,
                                        NULL,
                                        [[anError localizedDescription] UTF8String]);
	return ret;
}


+(NSError*) notImplemented: (NSString*) something
{
    return [[self class] errorWithDomain: SFC_ERROR_DOMAIN
                                    code: SFC_ERROR_NOT_IMPLEMENTED
                                userInfo: @{       NSLocalizedDescriptionKey: @"Not implemented",
                                            NSLocalizedFailureReasonErrorKey: something}];
}

+(NSError*) selfInitFail: (NSString*) className
{
    return [[self class] errorWithDomain: SFC_ERROR_DOMAIN
                                    code: SFC_ERROR_INIT_FAILED
                                userInfo: @{ NSLocalizedDescriptionKey: [NSString stringWithFormat: @"%@ init failed", className]}];

}

@end
